layui.use(['form','jquery','table','layer'],function() {
    let form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;



    let tabInstance = table.render({
        elem: '#dictionaryList',
        url: '/dataDictionary/search', //数据接口
        page: true, //开启分页
        limit:5, //每页显示的条数，默认10
        limits:[2,5,10,15,], //每页显示条数选择框数据
        request:{
            pageName: 'current',
            limitName: 'size'
        },
        cols: [[ //表头
            {field: 'id', title: '编号', width:80, sort: true, fixed: 'left'},
            {field: 'dataType', title: '类型'},
            {field: 'dataEntry', title: '条目'},
            {field: 'dataValue', title: '值'},
            {field: 'editAble', title: '是否可编辑',templet:function (user) {return user.editAble>0?'是':'否';
                }},
            {title: '操作', width: 135,fixed:'right',toolbar:"#userTool",hide:(0)}
        ]]

    });

    form.on('submit(search)',function(data){
        console.log(data.field);
        tabInstance.reload({
            where :data.field
        });
        return false;
    });


    //工具栏操作
    table.on('tool(test)',function(obj){
        switch (obj.event){
            case 'edit_btn':
                //编辑
                layer.open({
                    type: 2,
                    content: '/views/datadictionary/dataDictionaryAdd.jsp',
                    skin: 'layui-layer-molv',
                    title: "修改字典",
                    area: ["600px", "400px"],
                    btn: ['确定', '关闭'],
                    success:function (layero,index) {

                        console.log(obj.data.editAble);
                        let body = layer.getChildFrame('body',index);
                        body.find("#id").val(obj.data.id);
                        body.find("#dataType").val(obj.data.dataType);
                        body.find("#dataEntry").val(obj.data.dataEntry);
                        body.find("#dataValue").val(obj.data.dataValue);
                        body.find(".editAble input[value="+obj.data.editAble+"]").attr("checked","checked");
                    },
                    yes: function (index, layero) {
                        //user_edit
                        let user_form = layer.getChildFrame('body', index).find("#useradd");
                        let data = {};
                        //把数组转为JSON
                        user_form.serializeArray().forEach(function (val) {
                            data[val.name] = val.value;
                        });
                        $.post('/dataDictionary/edit', data, function (info) {
                            layer.close(index);
                            layer.msg(info.msg);
                            tabInstance.reload();
                        });
                    }
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除【'+obj.data.dataType+'】吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function(){
                    $.post('/dataDictionary/del/'+obj.data.id,function(data){
                        tabInstance.reload();
                        layer.msg(data.msg);
                    });
                });
        }
    });

    //添加
    $(".add_btn").click(function(){
        layer.open({
            type: 2,
            content: '/views/datadictionary/dataDictionaryAdd.jsp',
            skin: 'layui-layer-molv',
            title: "添加字典",
            area: ["600px", "400px"],
            btn: ['确定', '关闭'],

            yes: function (index, layero) {
                //user_edit
                let user_form = layer.getChildFrame('body', index).find("#useradd");
                let data = {};
                //把数组转为JSON
                user_form.serializeArray().forEach(function (val) {
                    data[val.name] = val.value;
                });
                $.post('/dataDictionary/add', data, function (info) {
                    console.log(data);
                    layer.close(index);
                    layer.msg(info.msg);
                    tabInstance.reload();
                });
            }
        });
    });


});